/*----------------------------------------------------------------------
  - File name      : STC8Ax_UART.h
  - Author         : Quark Team
  - Update date    : 2020-12-09                    
  -	Copyright      : Gevico Electronic studio   
  - Module comments: Header file of UART module.
-----------------------------------------------------------------------*/
#ifndef __STC8Ax_UART_H_
#define __STC8Ax_UART_H_
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
/*--------------------------------------------------------
| @Description: STC8Ax Options                           |
--------------------------------------------------------*/
#include "STC8Ax_REG.h"   
/*--------------------------------------------------------
| @Description: STC8Ax core                              |
--------------------------------------------------------*/
#include "STC8Ax_CORE.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/
/*--------------------------------------------------------
| @Description: System clock frequency                   |
--------------------------------------------------------*/
extern uint32 SYSCLK_FRE;
/*--------------------------------------------------------
| @Description: UART mode enum                           |
--------------------------------------------------------*/

typedef enum
{
	UART_SyncShift  = 0x00, //Mode0：Synchronous shift serial
	UART_8bit_BRTx  = 0x01,	//Mode1：8-bit data, variable baud rate
	UART_9bit_Fixed = 0x02, //Mode2：9-bit data, fixed baud rate
	UART_9bit_BRTx  = 0x03  //Mode3：9-bit data, variable baud rate
}	UARTMode_Type;

/*--------------------------------------------------------
| @Description: UART Baud rate generatorenum             |
--------------------------------------------------------*/

typedef enum
{
	UART_BRT_TIM1 = 0x00,  //Timer 1 as baud rate generator
	UART_BRT_TIM2 = 0x01,	 //Timer 2 as baud rate generator
	UART_BRT_TIM3 = 0x02,	 //Timer 3 as baud rate generator
	UART_BRT_TIM4 = 0x03 	 //Timer 4 as baud rate generator
}	UARTBRTGen_Type;

/*--------------------------------------------------------
| @Description: UART Baud rate Clock mode enum           |
--------------------------------------------------------*/
/*
Timing  1T mode: CPU clock without frequency division
Timing 12T mode: CPU clock 12 frequency division
*/
typedef enum
{
	UART_BRT_12T = 0,UART_BRT_1T
}	UARTBRTMode_Type;

/*--------------------------------------------------------
| @Description: UART init struct                         |
--------------------------------------------------------*/

typedef struct 
{
  UARTMode_Type Mode;        //Working mode
  UARTBRTGen_Type BRTGen;    //Baud rate generator
  UARTBRTMode_Type BRTMode;  //Baud rate generator clock mode
  FUNSTATE BRTDouble;        //Double baud rate
  uint32 BaudRate;           //BaudRate 
  FUNSTATE MulitComm; 		   //Multi mcu communication
  FUNSTATE Relay; 		       //Relay broadcasting
  FUNSTATE RxEnable;         //Receive enable 
} UART_InitType;

/*-----------------------------------------------------------------------
|                             API FUNCTION                              |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: UART busy flag of receive                |
--------------------------------------------------------*/

extern uint8 UART_BUSY_FLAG[4];		

#define      UART1_GET_BUSY_FLAG()    UART_BUSY_FLAG[0]
#define      UART2_GET_BUSY_FLAG()    UART_BUSY_FLAG[1]
#define      UART3_GET_BUSY_FLAG()    UART_BUSY_FLAG[2]
#define      UART4_GET_BUSY_FLAG()    UART_BUSY_FLAG[3]

#define      UART1_SET_BUSY_FLAG()    UART_BUSY_FLAG[0] = 1
#define      UART2_SET_BUSY_FLAG()    UART_BUSY_FLAG[1] = 1
#define      UART3_SET_BUSY_FLAG()    UART_BUSY_FLAG[2] = 1
#define      UART4_SET_BUSY_FLAG()    UART_BUSY_FLAG[3] = 1

#define    UART1_CLEAR_BUSY_FLAG()    UART_BUSY_FLAG[0] = 0
#define    UART2_CLEAR_BUSY_FLAG()    UART_BUSY_FLAG[1] = 0
#define    UART3_CLEAR_BUSY_FLAG()    UART_BUSY_FLAG[2] = 0
#define    UART4_CLEAR_BUSY_FLAG()    UART_BUSY_FLAG[3] = 0

/*--------------------------------------------------------
| @Description: UART busy flag of receive                |
--------------------------------------------------------*/

#define     CHAR_MAX     64

/*--------------------------------------------------------
| @Description: UART init function                       |
--------------------------------------------------------*/

FSCSTATE UART1_Init(const UART_InitType *UARTx);
FSCSTATE UART2_Init(const UART_InitType *UARTx);
FSCSTATE UART3_Init(const UART_InitType *UARTx);
FSCSTATE UART4_Init(const UART_InitType *UARTx);

/*--------------------------------------------------------
| @Description: UART working function                    |
--------------------------------------------------------*/

void UART1_Send_Byte(uint8 Dat);
void UART2_Send_Byte(uint8 Dat);
void UART3_Send_Byte(uint8 Dat);
void UART4_Send_Byte(uint8 Dat);

void UART1_Rev_Byte(uint8 *Dat);
void UART2_Rev_Byte(uint8 *Dat);
void UART3_Rev_Byte(uint8 *Dat);
void UART4_Rev_Byte(uint8 *Dat);

void UART1_Send_String(const uint8 *Str);
void UART2_Send_String(const uint8 *Str);
void UART3_Send_String(const uint8 *Str);
void UART4_Send_String(const uint8 *Str);

void UART1_Send_Array(const uint8 *Str,uint16 Len);
void UART2_Send_Array(const uint8 *Str,uint16 Len);
void UART3_Send_Array(const uint8 *Str,uint16 Len);
void UART4_Send_Array(const uint8 *Str,uint16 Len);


#endif
/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/

